// userInfoCtrl
(function() {
    angular.module('BlurAdmin.pages.UserlistM')
        .controller('userInfoCtrl', userInfoCtrl);

    function userInfoCtrl($scope, tongxin, $http, $myconfig) {
        var mobile = tongxin.getUserInfo();

        function getUserInfo() { //用户信息
            $http.get($myconfig.api.users_users_user_info + '?mobile=' + mobile).then(function(res) {
                // $scope.bigTotalItems = res.data.data;
                if (res.data.success) {
                    var o = 'assets/img/app/herd.png';
                    if (res.data.data.headimgurl) {
                        res.data.data.headimgurl = res.data.data.headimgurl.length > 3 ? ($myconfig.api.Qiniu_url + res.data.data.headimgurl) : (res.data.data.wxheadimgurl ? res.data.data.wxheadimgurl : o);
                    } else {
                        res.data.data.headimgurl = res.data.data.wxheadimgurl ? res.data.data.wxheadimgurl : o;
                    }


                    $scope.user_data = res.data.data;
                    console.log($scope.user_data);
                    qiaodao(res.data.data.id);
                    if (res.data.data.modality) {
                        res.data.data.modality[0] && getpar(res.data.data.modality[0].name, res.data.data.id);
                        res.data.data.modality[1] && getpar(res.data.data.modality[1].name, res.data.data.id);
                    }

                }
            });
        }

        getUserInfo();
        //获取用户签到天数
        function qiaodao(id) {
            $http.get($myconfig.api.users_users_signlogs_status + '?user_id=' + id).then(function(res) { //签到状态
                console.log(res);
                $scope.status = '上午签到';
                var oo = res.data.data;
                $scope.status = oo == 'Not_sign' ? "未签到" : (oo == 'AM_sign' ? '上午签到' : '下午签到');
            });
            $http.get($myconfig.api.users_users_sign_days + '?user_id=' + id).then(function(res) { //签到天数
                console.log(res);
                $scope.signed = res.data.data;
            })

        }


        var jbg_data = {
            name: '使用时长',
            datapoints: [
                { x: 2001, y: 100 },
                { x: 2002, y: 100 },
                { x: 2003, y: 100 },
                { x: 2004, y: 100 }

            ]
        };

        var yzb_data = {
            name: '使用时长',
            datapoints: [
                { x: 2001, y: 12 },
                { x: 2002, y: 155 },
                { x: 2003, y: 102 }
            ]
        };

        //获取用户治疗饼状图数据
        function getpar(name, id) {
            $http.get($myconfig.api.users_users_count_mode + '?dev_name=' + name + '&user_id=' + id).then(function(res) {
                // $scope.bigTotalItems = res.data.data;
                if (res.data.success) {
                    // res.data.data;
                    if (name == '金鼻光') {
                        $scope.jbg_data = res.data.data;
                        $scope.jbg_legend_name = [];
                        // jbg_data.datapoints = res.data.data;
                        // slice(start, end)
                        var arr = [
                            { x: 2001, y: 12 },
                            { x: 2002, y: 155 },
                            { x: 2003, y: 102 },
                            { x: 2003, y: 102 }
                        ];
                        var i = 0;
                        var s = 0;
                        for (i; i < res.data.data.length; i++) {
                            arr[i].y = ~~(res.data.data[i].durations / 60);
                            arr[i].x = res.data.data[i].mode;
                            $scope.jbg_legend_name.push(res.data.data[i].mode);
                            s += parseInt(res.data.data[i].durations);
                        }
                        $scope.jbg_s = Math.floor(s / 60);
                        jbg_data.datapoints = arr.slice(0, i);
                        $scope.opt1 = oo1;
                    }
                    if (name == '焕颜宝') {
                        $scope.yzb_data = res.data.data;
                        $scope.yzb_legend_name = [];

                        var arr = [{ x: 2001, y: 12 },
                            { x: 2002, y: 155 }, { x: 2003, y: 102 }, { x: 2003, y: 102 }
                        ];
                        var s = 0;
                        for (var i = 0; i < res.data.data.length; i++) {
                            arr[i].y = ~~(res.data.data[i].durations / 60);
                            arr[i].x = res.data.data[i].mode;
                            $scope.yzb_legend_name.push(res.data.data[i].mode);
                            s += parseInt(res.data.data[i].durations);

                        }
                        $scope.yzb_s = Math.floor(s / 60);
                        yzb_data.datapoints = arr.slice(0, i);
                        $scope.opt = oo2;
                    }
                    console.log(res.data.data);
                }
            });

        }





        //饼状图数据
        $scope.tpo = 1;
        $scope.myTabs = function(val) {
            $scope.tpo = val;
        }

        $scope.jbg_legend_name = [];
        var oo1 = {
            title: {
                text: '焕颜宝',
                x: 'center'
            },
            tooltip: {
                trigger: 'item',
                formatter: "{a} <br/>{b} : {c}"
            },
            legend: {
                //  show: false,
                tooltip: {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c}"
                },
                orient: 'horizontal',
                y: 'bottom',
                x: 'center',
                padding: '20',
                data: $scope.jbg_legend_name
            },
            series: [{
                name: '访问来源',
                type: 'pie',
                radius: '55%',
                center: ['50%', '60%'],
                data: [
                    { x: 2001, y: 100 },
                    { x: 2002, y: 100 },
                    { x: 2003, y: 100 },
                    { x: 2004, y: 100 }
                ]
            }]
        };
        $scope.jbg_config = $scope.opt1;
        $scope.data = false;
        $scope.data2 = false;



        var text = '焕颜宝';

        $scope.yzb_legend_name = [];
        $scope.opt;
        var oo2 = {
            title: {
                text: text,
                x: 'center'
            },
            tooltip: {
                trigger: 'item',
                formatter: "{a} <br/>{b} : {c}"
            },
            legend: {
                //  show: false,
                tooltip: {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c} "
                },
                orient: 'horizontal',
                y: 'bottom',
                x: 'center',
                padding: '20',
                data: $scope.yzb_legend_name
            },
            series: [{
                name: '访问来源',
                type: 'pie',
                radius: '55%',
                center: ['50%', '60%'],
                data: [
                    { x: 2001, y: 100 },
                    { x: 2002, y: 100 },
                    { x: 2003, y: 100 }

                ]
            }]
        };
        $scope.jbg_config2 = $scope.opt;

        setTimeout(function() {
            $scope.data = [jbg_data];
            $scope.data2 = [yzb_data];
            $scope.$apply();
        }, 600);

    }


})();